package com.sfy.test.demos.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component//需要在依赖项目中开启扫描 
public class FPTimeHandler implements MetaObjectHandler {
    /**
     * 新增数据自动填充
     * @param metaObject 元对象
     */
    @Override
    public void insertFill(MetaObject metaObject) {

        if (metaObject.hasSetter("createTime")){
            metaObject.setValue("createTime", new Date());
        }

        if (metaObject.hasSetter("updateTime")){
            metaObject.setValue("updateTime", new Date());
        }
    }

    /**
     * 更新数据自动填充
     * @param metaObject 元对象
     */
    @Override
    public void updateFill(MetaObject metaObject) {

        if (metaObject.hasSetter("updateTime")){
            metaObject.setValue("updateTime", new Date());
        }
    }
}